package com.tange.ai.core.sample.firmware

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tange.ai.core.sample.R
import com.tange.ai.core.sample.databinding.ActivityFirmwareUpgradeBinding
import com.tange.ai.core.sample.databinding.ActivityMessageSettingBinding
import com.tange.base.toolkit.*
import com.tange.base.ui.design.BasicDialogLoading
import com.tange.base.ui.design.BasicDialogRequest
import com.tange.core.cloud.message.CloudMessage
import com.tange.core.cloud.message.LocalPushConfigure
import com.tange.core.cloud.message.RemotePushConfigure
import com.tange.core.cloud.message.PushSwitch
import com.tange.core.device.manage.version.DeviceFirmware
import com.tange.core.device.manage.version.FirmwareVersion

class FirmwareUpgradeActivity : AppCompatActivity() {

    companion object {
        private const val TAG = "FirmwareUpgradeActivity_"
        private const val PARAM_DEVICE_ID = "PARAM_DEVICE_ID"

        fun launch(context: Context, deviceId: String) {
            context.startActivity(Intent(context, FirmwareUpgradeActivity::class.java).apply {
                putExtra(PARAM_DEVICE_ID, deviceId)
            })
        }
    }

    private lateinit var binding: ActivityFirmwareUpgradeBinding
    private lateinit var deviceId: String
    private var loading: BasicDialogLoading? = null

    private var firmwareVersion: FirmwareVersion? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarUtil.setLightStatusBar(this, true)
        StatusNavUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.app_background))
        requestWindowFeature(Window.FEATURE_NO_TITLE)

        deviceId = intent.getStringExtra(PARAM_DEVICE_ID)

        binding = ActivityFirmwareUpgradeBinding.inflate(layoutInflater).apply {
            setContentView(root)
            supportActionBar?.hide()
            root.paddingStatusBar()
        }

        binding.top.left.onClicked { finish() }

        loading = BasicDialogLoading(this).apply {
            setMessage("升级中，请稍候...")
        }


        binding.available.onClicked {
            firmwareVersion?.available?.let {

                loading?.show()

                DeviceFirmware(deviceId).performUpgrade(firmwareVersion!!) {
                    loading?.dismiss()

                    if (it.success) {
                        BasicDialogRequest(this@FirmwareUpgradeActivity).apply {
                            setMessage("升级成功，请等候设备重启")
                            setPositiveText("知道了")
                            setPositiveOnClicked {
                                dismiss()
                                finish()
                            }
                        }.show()
                    } else {
                        BasicDialogRequest(this@FirmwareUpgradeActivity).apply {
                            setMessage("升级失败： ${it.code}, ${it.message}")
                            setPositiveText("知道了")
                            setPositiveOnClicked {
                                dismiss()
                                finish()
                            }
                        }.show()
                    }
                }

            } ?: kotlin.run {
                Tips.show("无可用更新！")
            }
        }


        DeviceFirmware(deviceId).queryVersion { it ->

            if (!it.success) {
                Tips.show("查询固件信息失败：${it.code}, ${it.message}")
                return@queryVersion
            }

            it.data?.current?.let {
                binding.current.name.text = "当前版本：${it.version}"
                binding.current.arrow.gone()
            }

            it?.data?.available?.let {
                binding.available.name.text = "有新版本：${it.version}"
                binding.available.name.setTextColor(Color.RED)
                binding.available.desc.visible()
                binding.available.desc.text = "更新日志：${it.upgradeDesc}"
                binding.available.arrow.visible()
            } ?: kotlin.run {
                binding.available.arrow.gone()
                binding.available.name.text = "已是最新版本！"
            }

            firmwareVersion = it?.data
        }
    }
}